//
//  WYMainTabBar.m
//  openEye
//
//  Created by 徐悟源 on 16/3/31.
//  Copyright © 2016年 Wuyuan. All rights reserved.
//

#import "WYMainTabBar.h"

@interface WYMainTabBar()

@property (nonatomic, weak) UIButton *lastBarBtn;

@property (nonatomic, weak) UIView *lineView;


@end

@implementation WYMainTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        self.backgroundColor = WYColorWithAlpha(250, 250, 250, 0.7);
    }
    
    return self;
}

- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize osize = [super sizeThatFits:size];
    osize.height = 30;
    return osize;
}

- (void)addTabBarBtnWithName:(NSString *)name
{
    UIButton *btn = [[UIButton alloc] init];
    btn.titleLabel.font = Font_China(14);
    [btn setTitle:name forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected] ;
    [btn setTitleColor:WYColor(140, 140, 140) forState:UIControlStateNormal];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = WYColor(100, 100, 100);
    [btn addSubview:lineView];
    
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    
    [self addSubview:btn];
    if(self.subviews.count == 1)
    {
        [self btnClick:btn];
    }
}

- (void)btnClick:(UIButton *)btn
{
    
    if([self.delegate respondsToSelector:@selector(mainTabBar:didSelectBtnFrom:To:)])
    {
        [self.delegate mainTabBar:self didSelectBtnFrom:self.lastBarBtn.tag To:btn.tag];
    }
    
    self.lastBarBtn.selected = NO;
    btn.selected = YES;
    self.lastBarBtn = btn;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    for (int i = 0; i < self.subviews.count ; i++) {
        
        UIButton *btn = self.subviews[i];
        CGFloat btnW = self.frame.size.width / self.subviews.count;
        CGFloat btnH = self.frame.size.height;
        
        CGFloat btnX = i * btnW;
        CGFloat btnY = 0;
        
        btn.frame = CGRectMake(btnX, btnY , btnW,btnH );
        
        btn.tag = i;
        
        for (UIView *view  in btn.subviews) {
            if([view isKindOfClass:[UIView class]] && i > 0)
            {
                view.my_Width = 1;
                view.my_Height = TabBarH - (LineTopPading * 2);
                view.my_x = 1;
                view.my_centerY = btn.my_centerY;
            }
        }
        
        
    }
}
@end
